import { createStore, createLogger } from 'vuex';
import { queryUserInfo } from '@/api';

const env = process.env.NODE_ENV
const store = createStore({
    state: {
        profile: null
    },
    mutations: {
        queryProfile(state, profile) {
            state.profile = profile;
        }
    },
    actions: {
        async queryProfileAsync({ commit }) {
            let profile = null;
            try {
                let { code, data } = await queryUserInfo();
                if (+code !== 0) data = null;
                commit('queryProfile', data);
                profile = data;
            } catch (_) { }
            return profile;
        }
    },
    plugins: env !== 'production' ? [createLogger()] : []
})
export default store